package com.ruoyi.web.controller.initData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dao.SysDeptDao;
import com.ruoyi.system.domain.dao.SysRoleDao;
import com.ruoyi.system.mapper.OaMapper;
import com.ruoyi.system.mapper.SysDeptDaoMapper;
import com.ruoyi.system.mapper.SysRoleDaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DeptDutyRoleInit {

    @Autowired
    private OaMapper oaMapper;
    @Autowired
    private SysDeptDaoMapper sysDeptDaoMapper;
    @Autowired
    private SysRoleDaoMapper sysRoleDaoMapper;

    /**
     * 初始化 部门负责人角色，分管领导角色
     */
    public void initLeaderAndCharge() {
        List<SysDeptDao> list = sysDeptDaoMapper.selectList(new QueryWrapper<>());
        if (list != null && list.size() > 0) {
            list.stream().forEach(item -> {
                // depUuid=2c7b5f6cbec64845be545d57916db80a,dutyUuid=05438131dbce41dd99a304de294b9b52
                if (StringUtils.isEmpty(item.getOldData())) {
                    return;
                }
                String[] split = item.getOldData().split(",");
                if (split.length >2) {
                    String dutyUuid = split[1].split("=")[1];
                    String topLeaderDutyUuid = split[2].split("=")[1];
                    // 通过 dutyUuid 查询角色
                    List<Map<String, String>> listData = oaMapper.listDataByCondition("sys_t_role", "ent_t_duty_uuid", dutyUuid);
                    if (listData != null && listData.size() > 0) {
                        Map<String, String> val = listData.get(0);
                        String roleUuid = val.get("sys_t_role_uuid"); // 旧数据所在部门
                        //roleUuid=abcdefghijklmnopqrstuvwxyz
                        LambdaQueryWrapper lw = new LambdaQueryWrapper<SysRoleDao>()
                                .likeRight(SysRoleDao::getOldData, String.format("roleUuid=%s", roleUuid));
                        SysRoleDao sysRoleDao = sysRoleDaoMapper.selectOne(lw);
                        if (sysRoleDao != null) {
                            item.setDeptLeaderRole(sysRoleDao.getRoleId());
                        }
                    }
                    listData = oaMapper.listDataByCondition("sys_t_role", "ent_t_duty_uuid", topLeaderDutyUuid);
                    if (listData != null && listData.size() > 0) {
                        Map<String, String> val = listData.get(0);
                        String roleUuid = val.get("sys_t_role_uuid"); // 旧数据所在部门
                        //roleUuid=abcdefghijklmnopqrstuvwxyz
                        LambdaQueryWrapper lw = new LambdaQueryWrapper<SysRoleDao>()
                                .likeRight(SysRoleDao::getOldData, String.format("roleUuid=%s", roleUuid));
                        SysRoleDao sysRoleDao = sysRoleDaoMapper.selectOne(lw);
                        if (sysRoleDao != null) {
                            item.setDeptDeputyInChargeRole(sysRoleDao.getRoleId());
                        }
                    }
                    sysDeptDaoMapper.updateById(item);
                }
            });
        }
    }
}
